﻿//Ajax对象
function createXHR() {
    var xhr = null;
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		xhr = new ActiveXobject("Micrssoft.XMLHTTP");
	}
	return xhr;
}

//类insertBefore
function insertAfter(new_o, old_o) {
	var parent = old_o.parentNode;
	if (parent.lastChild == old_o) {
		parent.appendChild(new_o);
	} else {
		parent.insertBefore(new_o, old_o.nextSibling);
	}
}

//删除元素
function removeElement(obj){
    var parO = obj.parentNode;
    if(parO){
    	parO.removeChild(obj);  
    } else {
    	return false;
    }
}

//获取页面名称
function getPagename() {
		var filename = window.location.href;
		var pagename = filename.substr(filename.lastIndexOf("/") + 1);
		pagename = pagename.substr(0, pagename.indexOf("."));
		return pagename;
}

/*创建下拉菜单*/
function createMenu(arr) {
	var divO = document.createElement("div");
	divO.setAttribute("class", "zicaidan");
	var ulO = document.createElement("ul");
	for(var i = 0; i < arr.length; i++) {
	    var liO = document.createElement("li");
	    var aO = document.createElement("a");
	    aO.href = "javascript:;";
	    aO.appendChild(document.createTextNode(arr[i]));
	    liO.appendChild(aO);
	    ulO.appendChild(liO);
	}
	divO.appendChild(ulO);
	return divO;
}

/*创建资源列表*/
function createList(arr, id) {
	var divO = document.createElement("div");
	divO.className = "ziyuan";
	var ulO = document.createElement("ul");
	for(var i = 0; i < arr.length; i++){
		var liO = document.createElement("li");
		if(arr[0] == false) {
		    pO = document.createElement("p");
		    pO.innerHTML = "尚无资源,敬请期待！";
		    liO.appendChild(pO);
		} else {
		    liO.appendChild(document.createTextNode(i + 1 + "." + arr[i]));
		    aO = document.createElement("a");
			aO.setAttribute("href", "javascript:;");
			aO.innerHTML = "链接地址";
			aO.className = "lianjie";
			aO.setAttribute("data-id", id[i]);
			aO.setAttribute("data-click", "true");
			liO.appendChild(aO);	    					
		}
		    ulO.appendChild(liO);	    					
	}
		divO.appendChild(ulO);
		return divO;
}

/*创建详细信息列表*/
function createInfo(arr) {
	var divO = document.createElement("div");
	divO.className = "lianjiedizhi";
	var ulO = document.createElement("ul");
	var liO = document.createElement("li");
	liO.innerHTML = "文件名 " + arr[0];
	ulO.appendChild(liO);
	var liO = document.createElement("li");
	liO.innerHTML = "更新时间 " + arr[1];
	ulO.appendChild(liO);
	var liO = document.createElement("li");
	liO.innerHTML = "贡献者 " + arr[2];
	ulO.appendChild(liO);
	var liO = document.createElement("li");
	liO.innerHTML = "链接 " + arr[3];
	ulO.appendChild(liO);
	divO.appendChild(ulO);
	return divO;
}

/*改变a标签的点击事件*/
	var ys = document.querySelectorAll(".zuoce h3 a");
	var active = null,
		xhrMenu = null, 
		xhrList = null, 
		xhrInfo = null;
		
	if(ys) {
			for(var i = 0; i < ys.length ; i++) {
		ys[i].onclick = function() {
			/*判断是a是否点击过,未点击则创建*/
			if(this.getAttribute("data-click") == "true") {
				active = this;

				if(xhrMenu == undefined){
					var xhrMenu = createXHR();
					xhrMenu.open("POST", "/index.php/Index/getmenu", true);
	    			xhrMenu.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    			xhrMenu.onreadystatechange = function() {
	    				if(this.readyState == 4 && this.status == 200) {
	    					active.parentNode.style.borderRadius = "5px 5px 0px 0px";
	    					/*返回信息处理成数组*/
	    					var res = this.responseText.split(",");
							insertAfter(createMenu(res), active.parentNode);
							if(active.removeAttribute("data-click")) {
								active.removeAttribute("data-click");
							}
							var ys = document.querySelectorAll(".zicaidan ul li a");
							for(var i = 0; i < ys.length ; i++) {
								ys[i].onclick = function() {
									document.querySelector(".qianyan").setAttribute("style", "display:none");
									document.querySelector(".fenlei").removeAttribute("style");
									if(document.querySelector(".ziyuan")) {
										var ys = document.querySelector(".ziyuan");
		    							removeElement(ys);
		    						}
		    						if(xhrList == undefined) {
		    							var xhrList = createXHR();
		    							xhrList.open("POST", "/index.php/Index/getlist", true);
		    							xhrList.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		    							xhrList.onreadystatechange = function() {
		    								if(this.readyState == 4 && this.status == 200) {
		    									var res_fir = this.responseText.substr(0, this.responseText.indexOf("^"));
		    									var res_sec = this.responseText.substring(this.responseText.indexOf("^") + 1);
		    									res_fir = res_fir.split(",");
		    									res_sec = res_sec.split(",");
		    									insertAfter(createList(res_fir,res_sec), document.querySelector(".fenlei"));
		    									var ys = document.querySelectorAll(".lianjie");
		    									for(var i = 0; i < ys.length; i++) {
		    										ys[i].onclick = function() {
		    											var active = this;
		    											if(active.getAttribute("data-click") == "true") {
		    												if(xhrInfo == undefined) {
				    											var xhrInfo = createXHR();
					    										xhrInfo.open('POST', "/index.php/Index/getinfo", true);
					    										xhrInfo.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
					    										xhrInfo.onreadystatechange = function() {
					    											if(this.readyState == 4 && this.status == 200) {
					    												res = this.responseText.split(",");
					    												insertAfter(createInfo(res), active);
					    												if(active.removeAttribute("data-click")) {
																			active.removeAttribute("data-click");
			    														}
					    											}
					    										}
					    										xhrInfo.send("id=" + active.getAttribute("data-id"));
															}
		    											} else {
		    												if(this.getAttribute("data-iscoll") != "true") {
			    												this.nextSibling.style.display = "none";
			    												this.setAttribute("data-iscoll", "true");
			    											} else {
			    												this.nextSibling.style.display = "block";
			    												this.removeAttribute("data-iscoll");
			    											}
		    											}
		    										}
		    									}
		    								}
		    							}
		    							var lan = this.parentNode.parentNode.parentNode.previousSibling.firstChild.innerHTML;
		    							xhrList.send("lan=" + lan + "&menu=" + this.innerHTML + "&page=" + getPagename());
		    						}
								}
							}								
	    				}
	    			}
	    			xhrMenu.send("lan=" + this.innerHTML + "&page=" + getPagename());
				}									
		    } else {
				if (this.className == "data-collapsed") {
					this.parentNode.style.borderRadius = "5px 5px 0px 0px";	
					this.parentNode.nextSibling.style.display = "block";
					this.removeAttribute("class");
				} else {
					this.className = "data-collapsed";
					this.parentNode.nextSibling.style.display = "none";
					this.parentNode.style.borderRadius = "5px";						
				}				
		    }
		} 																	
	}
}



